<?php

namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\crm_deal;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\GongyingshangGender;

class Crm_gongyingshangController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){
            $content->header('供应商统计');
            $content->description('统计');
            $maoli=DB::table('crm_supplier')->get();
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $maoli[$k]->renshu=0;
                    $maoli[$k]->dakuan=0;
                    $deal=DB::table("crm_deal_crm_supplier")->where("crm_supplier_id",$vo->id)->pluck("crm_deal_id");
                    $renshu=DB::table("crm_deal")->whereIn("id",$deal)->where("deal_jiesuanstatue",3)->get();
                    if(!$deal->isEmpty() && !$renshu->isEmpty()){
                        foreach($renshu as $k=>$vo){
                            $a[$k]=$vo->deal_num;
                        }
                        $maoli[$k]->renshu=array_sum($a);
                    }
                    if(!$deal->isEmpty() && !$renshu->isEmpty()){
                        foreach($renshu as $k=>$vo){
                            $a[$k]=$vo->deal_sdakuan;
                        }
                        $maoli[$k]->dakuan=array_sum($a);
                    }
                }
            }
            $res['list']=$maoli;
            $content->body(new GongyingshangGender($res));
        });
    }
    // 表单提交后
    public function chengjiao(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {
            $content->header('供应商统计');
            $content->description('统计');
            $list=DB::table('crm_supplier')->paginate(1);
            $maoli=DB::table('crm_supplier')->get();
            $query=DB::table('crm_supplier');
            if($request['supplier_logoname']){
                $list=DB::table('crm_supplier')->where('supplier_logoname','like','%'.$request['supplier_logoname'].'%')->paginate(1);
                $maoli=DB::table('crm_supplier')->where('supplier_logoname','like','%'.$request['supplier_logoname'].'%')->get();
                $query=DB::table('crm_supplier')->where('supplier_logoname','like','%'.$request['supplier_logoname'].'%');
            }
            if($request['supplier_name']){
                $list=$query->where('supplier_name','like','%'.$request['supplier_name'].'%')->paginate(1);
                $maoli=$query->where('supplier_name','like','%'.$request['supplier_name'].'%')->get();
                $query=$query->where('supplier_name','like','%'.$request['supplier_name'].'%');
            }
            
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $maoli[$k]->renshu=0;
                    $maoli[$k]->dakuan=0;
                    if($request['order_lvyoudate']){
                        if($request['order_lvyoudate']['start']){
                            $deal=DB::table("crm_deal_crm_supplier")->where("crm_supplier_id",$vo->id)->pluck("crm_deal_id");
                            $renshu=DB::table("crm_deal")->whereIn("id",$deal)->where("deal_jiesuanstatue",3)->where('deal_dealdate', '>', substr($request['order_lvyoudate']['start'], 2))->get();
                            }
                        if($request['order_lvyoudate']['end']){
                           $deal=DB::table("crm_deal_crm_supplier")->where("crm_supplier_id",$vo->id)->pluck("crm_deal_id");
                            $renshu=DB::table("crm_deal")->whereIn("id",$deal)->where("deal_jiesuanstatue",3)->where('deal_dealdate', '<',substr($request['order_lvyoudate']['end'], 2))->get();
                        }
                    }else{
                        $deal=DB::table("crm_deal_crm_supplier")->where("crm_supplier_id",$vo->id)->pluck("crm_deal_id");
                        $renshu=DB::table("crm_deal")->whereIn("id",$deal)->where("deal_jiesuanstatue",3)->where("")->get();
                    }
                    if(!$deal->isEmpty() && !$renshu->isEmpty()){
                        foreach($renshu as $k=>$vo){
                            $a[$k]=$vo->deal_num;
                        }
                        $maoli[$k]->renshu=array_sum($a);
                    }
                    if(!$deal->isEmpty() && !$renshu->isEmpty()){
                        foreach($renshu as $k=>$vo){
                            $a[$k]=$vo->deal_sdakuan;
                        }
                        $maoli[$k]->dakuan=array_sum($a);
                    }
                }
            }
            $res['list']=$maoli;
            $content->body(new GongyingshangGender($res));
        });

    }
  

}
